package Game;
import java.awt.*;
import java.io.Serializable;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import GUIInterfaces.OregonPanel;
import GUIInterfaces.OregonWindow;


public class GUIControl extends JFrame implements OregonWindow,Serializable {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	private Caravan caravan;
	public static void start(GameDriver gd) {
		GUIControl frame = new GUIControl(gd);
		frame.setVisible(true);
				
	}
	
	/**
	 * Create the frame.
	 */
	CardLayout cardManager = new CardLayout();
	JPanel cards = new JPanel();
	ConfigPanel config;
	StorePanel store;
	MapPanel map;
	WinPanel win;
	GameDriver gd;
	ActionPanel actions;
	StartPanel start;
	RiverCrossingMinigame.GamePanel river;
	HuntGamePanel hunt;
	public GUIControl(GameDriver gd_p) {

		gd = gd_p;
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		contentPane = new JPanel();
		contentPane.setLayout(new BorderLayout(0, 0));
		
		reinitialize();
		
		cardManager.show(cards, "start");
		this.setBounds(0, 0, 991, 774);
		
		contentPane.add(cards);
		setContentPane(contentPane);
	}

	public void reinitialize() {
		config = new ConfigPanel(this,gd);
		store = new StorePanel(this);
		map = new MapPanel(this,gd);
		win = new WinPanel(this);
		actions = new ActionPanel(this);
		start = new StartPanel(this);
		river = new RiverCrossingMinigame.GamePanel(600, 300, new RiverCrossingMinigame.Grid(20,9),this);
		hunt = new HuntGamePanel(this,gd);
		addKeyListener(river);
		addKeyListener(hunt);
		cards.removeAll();
		cards.setLayout(cardManager);
		cards.add(config,"config");
		cards.add(store,"store");
		cards.add(map,"map");
		cards.add(win,"win");
		cards.add(actions,"actions");
		cards.add(start,"start");
		cards.add(river, "river");
		cards.add(hunt,"hunt");
	}
	
	public void done(OregonPanel op) {
		if (op == config) {
			Caravan tempCaravan = gd.getCaravan();
			tempCaravan.addVehicle(new Cart());
			gd.setCaravan(tempCaravan);
			store.setGameDriver(gd);
			
			cardManager.show(cards, "store");
		} else if (op == store) {
			gd.setCaravan(store.getGameDriver().getCaravan());
			//map.reset();
			cardManager.show(cards, "map");
			this.setBounds(0, 0, 991, 774);
			map.setGameDriver(gd);
		} else if (op == map) {
			String property = op.getProperty();
			gd.setCaravan(op.getGameDriver().getCaravan());
			if(property.equals("win")) {
				cardManager.show(cards, "win");
				this.setBounds(0, 0, 770, 594);
			} else if (property.equals("actions")) {
				cardManager.show(cards, "actions");
				gd = op.getGameDriver();
				
				actions.setGameDriver(gd);
				this.setBounds(0, 0, 499, 383);
			} else if (property.equals("river")) {
				cardManager.show(cards, "river");
				this.setBounds(0, 0, 600, 300);
			}
			
		} else if (op == win) {
			reinitialize();
			cardManager.show(cards, "config");
			
		} else if (op == actions) {
			String prop = op.getProperty();
			if (prop.equals("shop")) {
				store.setGameDriver(gd);
				this.setBounds(0, 0, 770, 594);
				cardManager.show(cards, "store");
			} else if (prop.equals("map")) {
				cardManager.show(cards, "map");
				this.setBounds(0, 0, 991, 774);
				map.setGameDriver(gd);
				
			} else if (prop.equals("settle")) {
				cardManager.show(cards, "win");
				this.setBounds(0, 0, 770, 594);
			}  else if (prop.equals("hunt")) {
				cardManager.show(cards, "hunt");
				this.setBounds(0, 0, 770, 594);
				addKeyListener(hunt);
				hunt.start();
			}
		} else if (op == start) {
			String prop = op.getProperty();
			if (prop.equals("new")) {
				cardManager.show(cards,"config");
				this.setBounds(0, 0, 770, 594);
			} else if (prop.equals("load")) {
				GameDriver gdtemp = new GameSave().loadGame();
		    	gd = gdtemp;
		    	gd.initializeLists();
		    	
				cardManager.show(cards,"map");
				this.setBounds(0, 0, 991, 774);
				map.setGameDriver(gd);
			}
		} else if (op == river) {
			String prop = op.getProperty();
			if (prop.equals("win")) {
				cardManager.show(cards,"map");
				this.setBounds(0, 0, 991, 774);
				map.setGameDriver(gd);
				map.update(-2);
			} else if (prop.equals("lose")) {
				cardManager.show(cards,"map");
				this.setBounds(0, 0, 991, 774);
				map.setGameDriver(gd);
			
			} 
		} else if (op == hunt) {
			cardManager.show(cards,"map");
			this.setBounds(0, 0, 991, 774);
			caravan = gd.getCaravan();
			int food = Integer.parseInt(op.getProperty());
			Inventory playInven = caravan.getPlayerInventory();
			playInven.addItem(GameDriver.getItemList().getIdOf("name", "Burrito"), food);
			caravan.setPlayerInventory(playInven);
			gd.setCaravan(caravan);
			map.setGameDriver(gd);
			hunt.stop();
		}
	}
}
